package com.intelligence.pda.ui.main;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.intelligence.androidlibrary.utils.SharePreferenceUtils;
import com.intelligence.pda.R;
import com.intelligence.pda.ui.BaseActivity;
import com.intelligence.pda.ui.abnormal.AbnormalActivity;
import com.intelligence.pda.ui.intercity.IntercityActivity;
import com.intelligence.pda.ui.landing.LandingMatchActivity;
import com.intelligence.pda.ui.scanlibrary.ScanTheLibraryActivity;
import com.intelligence.pda.ui.signout.SignOutActivity;
import com.intelligence.pda.utils.AppConstants;

import butterknife.Bind;

public class MainActivity extends BaseActivity implements View.OnClickListener {
    @Bind(R.id.tv_name)
    TextView tv_name;
    @Bind(R.id.ll_SignOut)
    LinearLayout ll_SignOut;
    @Bind(R.id.ll_landing_match)
    LinearLayout ll_landing_match;
    @Bind(R.id.ll_scan)
    LinearLayout ll_scan;
    @Bind(R.id.ll_intercity)
    LinearLayout ll_intercity;
    @Bind(R.id.ll_abnormal)
    LinearLayout ll_abnormal;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        listener();
    }

    private void listener() {
        ll_SignOut.setOnClickListener(this);
        ll_landing_match.setOnClickListener(this);
        ll_scan.setOnClickListener(this);
        ll_intercity.setOnClickListener(this);
        ll_abnormal.setOnClickListener(this);
    }

    private void initView() {
        tv_name.setText(SharePreferenceUtils.getString(AppConstants.PERSON_NAME));

    }

    @Override
    protected void onResume() {
        super.onResume();
        tv_name.setText(SharePreferenceUtils.getString(AppConstants.PERSON_NAME));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_SignOut:
                //设置
                startActivity(new Intent(mActivity, SignOutActivity.class));
                break;
            case R.id.ll_landing_match:
                //落地配入口
                startActivity(new Intent(mActivity, LandingMatchActivity.class));
                break;
            case R.id.ll_scan:
                //扫描出口
                startActivity(new Intent(mActivity, ScanTheLibraryActivity.class));
                break;
            case R.id.ll_intercity:
                //城际入口
                startActivity(new Intent(mActivity, IntercityActivity.class));
                break;
            case R.id.ll_abnormal:
                //异常登记
                startActivity(new Intent(mActivity, AbnormalActivity.class));
                break;
        }

    }

    /**
     * 退出登录
     */
    private void getQuit() {
        Intent intent = new Intent(mActivity, MainActivity.class);
        startActivity(intent);
        activityManagerUtil.finishAllActivity();
    }


    //双击返回退出程序
    private long firstTime = 0;

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                long secondTime = System.currentTimeMillis();
                if (secondTime - firstTime > 2000) {//如果两次按键时间间隔大于2秒，则不退出
                    Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
                    firstTime = secondTime;//更新firstTime
                    return true;
                } else { //两次按键小于2秒时，退出应用
                    System.exit(0);
                }
                break;
        }
        return super.onKeyUp(keyCode, event);
    }
}
